﻿@using SmartStore.Admin.Models.Localization;
@model List<LanguageModel>
@{
    ViewBag.Title = T("Admin.Configuration.Languages").Text;

    var defaultLanguageNote = ViewBag.DefaultLanguageNote as string;
}
<div class="section-header">
    <div class="title">
        <i class="fa fa-language"></i>
        @T("Admin.Configuration.Languages")
    </div>
    <div class="options">
        <a href="@Url.Action("Create")" class="btn btn-primary">
			<i class="fa fa-plus"></i>
			<span>@T("Admin.Common.AddNew")</span>
		</a>
    </div>
</div>

<div>
	<h5 class="mb-3">@T("Admin.Configuration.Languages.InstalledLanguages")</h5>
    @if (defaultLanguageNote.HasValue())
    {
        <div class="alert alert-info">
            @Html.Raw(defaultLanguageNote)
        </div>
    }

	<div class="table-responsive">
		<table class="table admin-table installed-languages-list language-list">
			<thead>
				<tr>
					<th style="width: 350px;">@T("Admin.Configuration.Languages.Fields.Name")</th>
					<th style="width: 160px;" class="text-center">@T("Admin.Configuration.Languages.Fields.LanguageCulture")</th>
					<th style="width: 140px;" class="text-center">@T("Admin.Configuration.Languages.Fields.UniqueSeoCode")</th>
					<th style="width: 160px;" class="text-center">@T("Admin.Configuration.Languages.Fields.Published")</th>
					<th style="width: 200px;">@T("Admin.Configuration.Languages.Fields.LastResourcesImportOn")</th>
					<th style="width: 160px;" class="text-center">@T("Admin.Common.Store.LimitedTo")</th>
					<th class="text-center">@T("Common.DisplayOrder")</th>
					<th></th>
				</tr>
			</thead>
			<tbody>
				@foreach (var language in Model)
				{
					<tr class="@(language.Published ? "" : "disabled")">
						<td>
							@if (language.FlagImageFileName.HasValue())
							{
								<img src="@Url.Content("~/Content/images/flags/" + language.FlagImageFileName)" class="flag-icon mr-1 mb-1" />
							}
							<a href="@Url.Action("Edit", new { id = language.Id })">@language.Name</a>
						</td>
						<td class="text-center">
							@language.LanguageCulture
						</td>
						<td class="text-center">
							@language.UniqueSeoCode
						</td>
						<td class="text-center">
							@Html.SymbolForBool(language.Published)
						</td>
						<td>
							@if (language.LastResourcesImportOn.HasValue)
							{
								<span title="@language.LastResourcesImportOn.Value.ToString()">@language.LastResourcesImportOnString</span>
							}
							else
							{
								@Html.SymbolForBool(false)
							}
						</td>
						<td class="text-center">
							@Html.SymbolForBool(language.LimitedToStores)
						</td>
						<td class="text-center">
							@language.DisplayOrder
						</td>
						<td class="omega text-truncate">
							<a href="@Url.Action("Edit", new { id = language.Id })" class="btn btn-warning btn-sm">
								<span>@T("Admin.Common.Edit")</span>
							</a>
							<a href="@Url.Action("Resources", new { languageId = language.Id })" class="btn btn-secondary btn-sm">
								<span>@T("Admin.Configuration.Languages.Resources.View")</span>
							</a>
						</td>
					</tr>
				}
			</tbody>
		</table>
	</div>
</div>

<div class="mt-5">
	<h5 class="mb-3">@T("Admin.Configuration.Languages.AvailableLanguages")</h5>
    <div id="AvailableLanguages"></div>
</div>

<script type="text/javascript">
    $(document).ready(function () {

        // Load available languages.
	    $({}).doAjax({
	        url: '@Url.Action("AvailableLanguages")',
	        smallIcon: '#AvailableLanguages',
	        callbackSuccess: function (resp) {
	            $('#AvailableLanguages').html(resp);
	        }
	    });

        // Watch language download progress.
		SmartStore.Admin.TaskWatcher.startWatching({
            context: $('#AvailableLanguages'),
            elementsSelector: '.task-progress',
            defaultProgressMessage: @T("Admin.System.ScheduleTasks.RunNow.IsRunning").JsText,
            pollUrl: '@Url.Action("DownloadProgress")',
            onTaskStarted: function (task, el) {
                var tr = el.closest('tr');

                $('#AvailableLanguages').find('a.download-language').addClass('hide');
                $('body, html').animate({ scrollTop: tr.offset().top }, 'slow');
            },
            onTaskCompleted: function (taskId, el) {
               window.location = '@Url.Action("List")';
            }
        });
    });
</script>